package by.bsu.scs.util.form;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import org.apache.struts.action.Action;
import org.apache.struts.util.LabelValueBean;

import by.bsu.scs.db.dao.DisciplineInstructorHome;
import by.bsu.scs.db.dao.DisciplineTaskHome;
import by.bsu.scs.db.dao.MarkHome;
import by.bsu.scs.db.dao.TaskAttachmentHome;
import by.bsu.scs.db.dao.TaskAttachmentStateHome;
import by.bsu.scs.db.dao.TaskHome;
import by.bsu.scs.model.DisciplineInstructor;
import by.bsu.scs.model.Mark;
import by.bsu.scs.model.Task;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.model.TaskAttachmentState;
import by.bsu.scs.model.User;
import by.bsu.scs.presentation.form.TaskForm;
/**
 * 
 * @author Svetlana Voiteh
 * @version 1.0
 * 
 * Fill the elements of TaskForm for students
 */
public class StudentTaskFormFiller {

		
	private static final String CHECKED = "checked";
	private static final String UNCHECKED = "unchecked";
	private static final String TASK = "task";

	private TaskForm form;
	private User student;

	public StudentTaskFormFiller(TaskForm form, User student) {
		super();
		this.form = form;
		this.student = student;
	}

	/*
	 * Fill the elements of TaskForm for students for all tasks
	 */
	public void fillElementsForAllTasks() {
		ArrayList<DisciplineInstructor> disciplineInstructorList = new ArrayList<DisciplineInstructor>();
		ArrayList<Task> taskList = new ArrayList<Task>();
		ArrayList<TaskAttachment> taskAttachmentList = new ArrayList<TaskAttachment>();
		ArrayList<Mark> markList = new ArrayList<Mark>();
		ArrayList<StudentTask> studentTaskList = new ArrayList<StudentTask>();
		ArrayList<LabelValueBean> disciplineList = new ArrayList<LabelValueBean>();

		disciplineInstructorList = (ArrayList<DisciplineInstructor>) (new DisciplineInstructorHome())
				.getDisciplineInstructorByStudentIdSQL(this.student);

		/*
		 *  fill list of disciplines
		 */
		for (DisciplineInstructor di : disciplineInstructorList) {
			disciplineList.add(new LabelValueBean(di.getDiscipline()
					.getDisciplineName(), new Integer(di.getDiscipline()
					.getDisciplineId()).toString()));
		}

		/*
		 *  fill list of tasks
		 */
		if(this.form.getDisciplineId() == 0){
			taskList = (ArrayList<Task>) new TaskHome()
					.getTasksByStudentIdSQL(this.student);
		}
		/*
		 *  if update
		 */
		else{
			taskList = (ArrayList<Task>) new DisciplineTaskHome()
			.getTasksByStudentAndDisciplineIdSQL(this.student, this.form.getDisciplineId());
		}

		TaskAttachment taskAttachment = new TaskAttachment();
		String filePath = null;
		int numberUpload;
		int mark;

		/*
		 * fill list of student tasks that need for view on jsp
		 */
		for (Task t : taskList) {
			mark = 0;
			filePath = new TaskAttachmentHome()
					.getFilePathOfTaskAttachmentByTaskAndState(t, TASK);

			taskAttachmentList = (ArrayList<TaskAttachment>) new TaskAttachmentHome()
					.getTaskAttachmentByStudentAndTask(this.student, t);
			numberUpload = taskAttachmentList.size();
			if (!taskAttachmentList.isEmpty()) {
				SortByTaskAttachmentId id = new SortByTaskAttachmentId();
				Collections.sort(taskAttachmentList, id);
				taskAttachment = taskAttachmentList.get(0);
				for (TaskAttachment ta : taskAttachmentList) {
					if (CHECKED.equalsIgnoreCase(ta.getTaskAttachmentState()
							.getState())) {
						taskAttachment = ta;
						break;
					}
				}
				markList = (ArrayList<Mark>) new MarkHome()
						.getMarksByTaskAttachment(taskAttachment);

				if (!markList.isEmpty()) {
					for (Mark m : markList) {
						mark += m.getMark();
					}
					mark /= markList.size();
				}
			}

			studentTaskList.add(new StudentTask(t, mark, numberUpload, new File(
					filePath)));
		}

		this.form.setMarkList(markList);
		this.form.setDisciplineInstructorList(disciplineInstructorList);
		this.form.setStudentTaskList(studentTaskList);
		this.form.setDisciplineList(disciplineList);

	}
	
	/*
	 * Fill the elements of TaskForm for students for unchecked tasks
	 */
	public void fillElementsForUncheckedTasks() {
		ArrayList<DisciplineInstructor> disciplineInstructorList = new ArrayList<DisciplineInstructor>();
		ArrayList<Task> taskList = new ArrayList<Task>();
		ArrayList<TaskAttachment> taskAttachmentList = new ArrayList<TaskAttachment>();
		ArrayList<StudentTask> studentTaskList = new ArrayList<StudentTask>();
		ArrayList<LabelValueBean> disciplineList = new ArrayList<LabelValueBean>();

		disciplineInstructorList = (ArrayList<DisciplineInstructor>) (new DisciplineInstructorHome())
				.getDisciplineInstructorByStudentIdSQL(this.student);

		/*
		 *  fill list of disciplines
		 */
		for (DisciplineInstructor di : disciplineInstructorList) {
			disciplineList.add(new LabelValueBean(di.getDiscipline()
					.getDisciplineName(), new Integer(di.getDiscipline()
					.getDisciplineId()).toString()));
		}

		/*
		 *  fill list of tasks
		 */
		if(this.form.getDisciplineId() == 0){
			taskList = (ArrayList<Task>) new TaskHome()
					.getTasksByStudentIdSQL(this.student);
		}
		/*
		 *  if update
		 */
		else{
			taskList = (ArrayList<Task>) new DisciplineTaskHome()
			.getTasksByStudentAndDisciplineIdSQL(this.student, this.form.getDisciplineId());
		}

		TaskAttachment taskAttachment = new TaskAttachment();
		String filePath = null;
		int numberUpload;
		
		/*
		 * fill list of student tasks that need for view on jsp
		 */
		for (Task t : taskList) {
			filePath = new TaskAttachmentHome()
					.getFilePathOfTaskAttachmentByTaskAndState(t, TASK);

			taskAttachmentList = (ArrayList<TaskAttachment>) new TaskAttachmentHome()
					.getTaskAttachmentByStudentAndTask(this.student, t);
			numberUpload = taskAttachmentList.size();
			if (!taskAttachmentList.isEmpty()) {
				SortByTaskAttachmentId id = new SortByTaskAttachmentId();
				Collections.sort(taskAttachmentList, id);
				taskAttachment = taskAttachmentList.get(0);
				if (UNCHECKED.equalsIgnoreCase(taskAttachment.getTaskAttachmentState()
							.getState())) {
					studentTaskList.add(new StudentTask(t, 0, numberUpload, new File(
							filePath)));
				}
			}
			else {
				studentTaskList.add(new StudentTask(t, 0, numberUpload, new File(
						filePath)));
			}
		}

		this.form.setDisciplineInstructorList(disciplineInstructorList);
		this.form.setStudentTaskList(studentTaskList);
		this.form.setDisciplineList(disciplineList);
	}
	
	/**
	 * Create solution
	 * 
	 * @param action
	 * @return TaskAttachment solution
	 */
	public TaskAttachment createSolution(Action action) {
		TaskAttachment solution = new TaskAttachment();
		solution.setTask(this.form.getStudentTask().getTask());
		FileManager fileUpload = new FileManager();
		solution.setTaskAttachmentName(fileUpload.upload(this.form.getSolutionFile(), action));
		TaskAttachmentState state = new TaskAttachmentState();
		state.setState(UNCHECKED);
		solution.setTaskAttachmentState((TaskAttachmentState) new TaskAttachmentStateHome().findByExample(state).get(0));
		solution.setUser(this.student);
		return solution;
	}
	
//	public void createPolicyDescription(){
//		StringBuilder policy = new StringBuilder();
//		Iterator<MarkPolicyCriteria> it = this.form.getStudentTask().getTask().getMarkPolicy().getMarkPolicyCriteria().iterator();
//		policy.append("Политика :" + this.form.getStudentTask().getTask().getMarkPolicy().getPolicyShort() + "\n");
//		policy.append("Описание :" + this.form.getStudentTask().getTask().getMarkPolicy().getPolicyDescription() + "\n");
//		policy.append("Критерии :\n");
//		while(it.hasNext()){
//			MarkPolicyCriteria criteria = it.next();
//			policy.append(criteria.getMarkCriteria().getCriteriaShort() + "(" + criteria.getMarkCriteria().getCriteriaDescription() + ")" + "\n");
//			
//		}
//		this.form.setPolicy(policy.toString());
//	}

}
